package com.unrarme.api.kinobazaapi
{
	import flash.utils.Dictionary;

	public class KinobazaRequest
	{
		public static const TYPE_MOVIE:String = 'movie';
		public static const TYPE_SERIES:String = 'series';
		
		public static const STATUS_NONE:String = 'none';
		public static const STATUS_SEEN:String = 'seen';
		public static const STATUS_DELETED:String = 'deleted';
		public static const STATUS_STARRED:String = 'starred';
		public static const STATUS_WATCHING:String = 'watching';
		
		public static const FIELD_IDENTIFIERS:Number = 0;
		public static const FIELD_BASIC:Number = 1;
		public static const FIELD_DESCRIPTION:Number = 2;
		public static const FIELD_PARTICIPANTS:Number = 4;
		public static const FIELD_GENRES:Number = 8;
		public static const FIELD_COUNTRY:Number = 16;
		public static const FIELD_RATINGS:Number = 32;
		public static const FIELD_FULL:Number = 63;
			
		public var ids: Array = new Array();
		public var type: String = null;
		public var status: String = null;
		public var fields_mask: Number = -1;
		public var genres: Array = new Array();
		public var genres_not: Array = new Array();
		public var genres_mode: String = null;
		public var languages: String = null;
		public var participants: Array = new Array();
		public var countries: Array = new Array();
		public var countries_not: Array = new Array();
		public var imdb_rating_max: Number = -1;
		public var imdb_rating_min: Number = -1;
		public var kinopoisk_rating_max: Number = -1;
		public var kinopoisk_rating_min: Number = -1;
		public var imdb_rating_voted_min: Number = -1;
		public var kinopoisk_rating_voted_min: Number = -1;
		public var year_max: Number = -1;
		public var year_min: Number = -1;
		public var qualities: Number = -1;
		public var sort: Number = -1;
		public var limit: Number = -1;
		public var offset: Number = -1;
		
		private var _request:Object;
		
		public function KinobazaRequest(request:Object){
			this._request = request;
		}
		
		public function set request(object:Object):void {
			this._request = object;
		}
		
		public function get request():Object {
			return this._request;
		}
		
		public function setPrameter(key:String, value:Object):void {
			this._request[key] = value;
		}
		
		public function getParameter(key:String):Object {
			return this._request[key];
		}
		
		private function a2s(array:Object, glue:String = ',', associate:Boolean = false, key:String=''):String {
			var result:String = '';
			if (associate) {
				for (var key:String in array) {
					result += (key + '=' + array[key] + glue);										
				}
			} else {
				for each (var value:String in array) {
					result += (key + '[]=' + value.toString() + glue);										
				}				
			}
			
			result = result.substring(0, result.length - 1);
			return result;
		}
		
		public function createRequest(): String {
			var req:Array = new Array();
			
			for (var key:String in this._request) {
				var value:* = this.request[key];
				if(typeof(value) == 'object') {
					req[key] = a2s(value, ',', false, key);
				} else {
					req[key] = value;
				}
			}
			
			return a2s(req, '&', true);
		}
	}
}